package com.asia.algorithmcode.str;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Stack;

/**
 * @DESCRIPTION:
 * @USER: wanfu
 * @DATE: 2025/3/13 星期四 10:34
 */
public class AddBinary {

    private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static void main(String[] args) {
//        System.out.println(new AddBinary().addBinary("1", "0"));


    }

    public String addBinary(String a, String b) {
        Stack<Integer> stack = new Stack();
        int m = a.length() - 1;
        int n = b.length() - 1;
        int add = 0;
        while (m >= 0 && n >= 0) {
            int av = Integer.parseInt(a.charAt(m) + "");
            int bv = Integer.parseInt(b.charAt(n) + "");
            int value = av + bv + add;
            if (value >= 2) {
                stack.push(value % 2);
                add = 1;
            } else {
                stack.push(value % 2);
                add = 0;
            }
            m--;
            n--;
        }
        while (m >= 0) {
            int value = Integer.parseInt(a.charAt(m) + "") + add;
            if (value >= 2) {
                stack.push(value % 2);
                add = 1;
            } else {
                stack.push(value % 2);
                add = 0;
            }
            m--;
        }
        while (n >= 0) {
            int value = Integer.parseInt(b.charAt(n) + "") + add;
            if (value >= 2) {
                stack.push(value % 2);
                add = 1;
            } else {
                stack.push(value % 2);
                add = 0;
            }
            n--;
        }
        if (add == 1) {
            stack.push(1);
        }
        String ans = "";
        while (!stack.isEmpty()) {
            ans += stack.pop();
        }
        return ans;
    }


}
